<?php

namespace App\Services\Permission;


use App\Models\Admin;
use Spatie\Permission\Models\Permission;
use MenuService;
use function PHPUnit\Framework\isEmpty;
use Illuminate\Support\Arr;
use Auth;

/**
 * 初始化网站所有的权限，后台获取权限并存入数据库
 * @package App\Services
 */
class PermissionService
{
    public function generate()
    {
        $permissions = include config_path() . '/permissions.php';
        foreach ($permissions as $key => $permission) {
            foreach ($permission['rules'] as $index => $rule) {
                //没有就添加，有就更新
                Permission::updateOrcreate(['name' => $rule['name'], 'title' => $rule['title']]);
            }
        }
    }

    public function access($name): bool
    {
        //如果ID为1，表示超级管理员，拥有所有权限
        if (Auth::id() == 1) {
            return true;
        }
        //当前管理员拥有权限的名称
        return Auth::user()->can($name);
    }
}
